/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package games.kamisado;

import minmax.MinMax;
import games.KamisadoState;
import java.util.ArrayList;
import java.util.Iterator;
import games.KamisadoState.KamisadoMove;
import games.SquareColor;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;

public class KamisadoMovesTest {

    @Test
    public void moves_fromStartState_brownShouldHave12Moves() throws FileNotFoundException, IOException {
        KamisadoState state = new KamisadoState();

        Iterator<KamisadoMove> result = state.moves();
        ArrayList<KamisadoMove> moves = new ArrayList<KamisadoMove>();
        while (result.hasNext()) {
            KamisadoMove step = result.next();
            if (step.getTower().getColor() == SquareColor.BROWN) {
                moves.add(step);
                System.out.println(step.toString());
            }
        }

        assertEquals(12, moves.size());

    }

    @Test
    public void moves_fromStartState_organgeShouldHave12Moves() throws FileNotFoundException, IOException {
        KamisadoState state = new KamisadoState();

        Iterator<KamisadoMove> result = state.moves();
        ArrayList<KamisadoMove> moves = new ArrayList<KamisadoMove>();
        while (result.hasNext()) {
            KamisadoMove step = result.next();
            if (step.getTower().getColor() == SquareColor.ORANGE) {
                moves.add(step);
                System.out.println(step.toString());
            }
        }

        assertEquals(12, moves.size());

    }

    @Test
    public void moves_fromStartState_pinkShouldHave13Moves() throws FileNotFoundException, IOException {
        KamisadoState state = new KamisadoState();

        Iterator<KamisadoMove> result = state.moves();
        ArrayList<KamisadoMove> moves = new ArrayList<KamisadoMove>();
        while (result.hasNext()) {
            KamisadoMove step = result.next();
            if (step.getTower().getColor() == SquareColor.PINK) {
                moves.add(step);
                System.out.println(step.toString());
            }
        }

        assertEquals(13, moves.size());

    }
    
    @Test
    public void moves_whenComputerIsForcedToMoveSameTowerTwice_ShouldNotCrash() throws FileNotFoundException, IOException {
        KamisadoState state = new KamisadoState();
        
        KamisadoMove move = new KamisadoMove(0, 5, 0, 7, state.getTower(0, 7));

        state.move(move);
        
        KamisadoMove move2 = new KamisadoMove(0, 1, 1, 0, state.getTower(1, 0));
        state.move(move2);
        
        KamisadoMove move3 = new KamisadoMove(3, 6, 2, 7, state.getTower(2, 7));

        state.move(move3);
        
        MinMax minMax = new MinMax(10);
        KamisadoMove m3 = minMax.minmax(state);
        assertNotNull(m3);
        Iterator<KamisadoMove> result = state.moves();
        ArrayList<KamisadoMove> moves = new ArrayList<KamisadoMove>();
        while (result.hasNext()) {
            KamisadoMove step = result.next();
                moves.add(step);
                System.out.println(step.toString());
        }
        


    }
    
    
    @Test
    public void moves_deadlock_nextMoveIsCorrect() {
       KamisadoState state = new KamisadoState();
       MoveBuilder builder = new MoveBuilder(state);
       
       state.move(builder.y(7, 6).x(5, 6).build()); //p1
       state.move(builder.y(0, 5).x(0, 5).build()); //p2
       
       state.move(builder.y(7, 3).x(7, 7).build());
       state.move(builder.y(0, 5).x(4, 4).build());
       
       state.move(builder.y(7, 3).x(2, 2).build());
       state.move(builder.y(0, 5).x(1, 6).build());
       
       state.move(builder.y(7, 5).x(4, 2).build());
       state.move(builder.y(0, 2).x(7, 7).build());

       state.move(builder.y(7, 6).x(6, 7).build());
       state.move(builder.y(0, 2).x(5, 3).build());

       state.move(builder.y(3, 2).x(2, 1).build());
       state.move(builder.y(0, 1).x(3, 4).build());

       state.move(builder.y(6, 4).x(7, 7).build());
       state.move(builder.y(1, 2).x(4, 4).build());

       state.move(builder.y(6, 5).x(6, 7).build());
       state.move(builder.y(0, 4).x(6, 6).build()); 
        //p2 makes the last move the rest will be a deadlock between
        /* Player 1 [mikael] moves:
        KamisadoMove{y=5, x=7, oldX=7, oldY=5, tower=Tower{color=RED, player=false}}
        Player 2 [computer] moves:
        KamisadoMove{y=4, x=6, oldX=6, oldY=4, tower=Tower{color=BLUE, player=true}} */
        Iterator<KamisadoMove> moves = state.moves();
        moves.hasNext();
        KamisadoMove next = moves.next();
        
        assertFalse(moves.hasNext());
        assertEquals(5, next.getY());
        assertEquals(7, next.getX());
        

    }
}
